<template>
  <el-dialog title="选择小区" :visible.sync="visible" width="900px" top="5vh" append-to-body>
    <el-card class="filter-container" shadow="never">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="添加时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="小区名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入小区名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-row>
      <div class="table-container">
        <el-table @current-change="singleElection" highlight-current-row :data="tableList"  height="260px" border>
          <el-table-column align="center" width="55" label="选择">
            <template slot-scope="scope">
              <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
              <el-radio class="radio" v-model="templateSelection" :label="scope.row.villageId">&nbsp;</el-radio>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="小区名称" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="address" label="小区地址" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="是否显示" align="center">
            <template slot-scope="scope">
              {{ scope.row.isShow === 1 ? "显示" : "隐藏"}}
            </template>
          </el-table-column>
          <el-table-column prop="createdTime" label="创建时间">
            <template slot-scope="scope">
              <span>{{ dateTimeFormat(scope.row.createdTime) }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页区-->
      <div class="pagination-container">
        <el-pagination
          layout="total, sizes,prev, pager, next,jumper"
          :current-page="queryParams.currentPage"
          :page-size="queryParams.pageSize"
          :page-sizes="[10,15,20]"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { listVillage } from "@/api/basic/village";
  export default {
    name: "SelectVillage",
    data() {
      return {
        // 遮罩层
        visible: false,
        // 总条数
        total: 0,
        // 表数据
        tableList: [],
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined
        },
        // 当前选择的行的id
        templateSelection: "",
        // 当前选择的行的数据
        checkData: {},
      }
    },
    methods: {
      // 显示弹框
      show() {
        this.getList();
        this.visible = true;
      },
      // 单选框选中数据
      singleElection(row) {
        this.templateSelection = row.villageId
        this.checkData = row
      },
      // 查询表数据
      getList() {
        listVillage(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
          if (res.code === 200) {
            this.tableList = res.rows;
            this.total = res.total;
          }
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSizeChange(val) {
        this.queryParams.pageNum = 1
        this.queryParams.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.queryParams.pageNum = val
        this.getList()
      },
      /** 确认选择 */
      confirm() {
        this.$emit("confirmVillage", this.checkData);
        this.visible = false;
      },
    }
  }
</script>
